package utilities;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import database.DBObject;

public class Edit {
	private DBObject database = new DBObject();

	public Edit() throws SQLException {
		database.connect();
	}

	public void editSubcategory(String subcategory, String columnValue, String newValue) throws SQLException {
		
		if(columnValue.equals("CURRENTYEARAMOUNT")){
		String myStatement = "Update budget set currentyearamount = "
				+ newValue
				+ " Where id IN(Select id from budgethassubcategory Where name = '"
				+ subcategory + "' )";
		database.executeAddOrEdit(myStatement);
		}
		else if (columnValue.equals("LOBBYINGEXP")){
			String myStatement = "Update subcategory set lobbyingexp = '"
					+ newValue
					+ "' Where name = '"
					+ subcategory + "'";
			database.executeAddOrEdit(myStatement);
		}
		
	}
	
	public void editPeople(String original, String tableName, String columnName,  String newValue, String email) throws SQLException{
		if(tableName.equals("Members")){
		if(columnName.equals("FIRSTNAME") || columnName.equals("LASTNAME")){
			String myState = "Update " + tableName +" set " + columnName + " = '" + newValue + "' where " + columnName + " = '" + original
				+ "'";
		database.executeAddOrEdit(myState);
		}
		else if(!(columnName.equals("EMAIL")) && !(columnName.equals("TYPE")) ){
			String myState = "Update contributors set " + columnName + " = '" + newValue + "' where email = '" + email 
					+ "'";
			database.executeAddOrEdit(myState);
		}
		else if(columnName.equals("TYPE")){
			String myState = "Update hasmembership set " + columnName +" = '" + newValue + "' where email = '" + email + "'";
			
			database.executeAddOrEdit(myState);
		}
		}else if(tableName.equals("Nonmembers")){
			if(columnName.equals("NAME") ){
				String myState = "Update " + tableName +" set " + columnName + " = '" + newValue + "' where " + columnName + " = '" + original
					+ "'";
			database.executeAddOrEdit(myState);
			}
			else if(!(columnName.equals("EMAIL")) ){
				String myState = "Update contributors set " + columnName + " = '" + newValue + "' where email = '" + email 
						+ "'";
				database.executeAddOrEdit(myState);
			}
		}
		
		
	}
	
	public void editMembershipTypes(String columnName,  String newValue, String type) throws SQLException{
		if(!(columnName.equals("TYPE"))){
			String myState = "Update membershiptype set " + columnName + " = " + newValue + " where type = '" + type +"'";
			database.executeAddOrEdit(myState);
		}
	}
	
	public void editVendors(String columnName,  String newValue, String email) throws SQLException{
		
		if(!(columnName.equals("EMAIL"))){
			String myState = "Update vendor set " + columnName + " = '" + newValue + "' where email = '" + email +"'";
			database.executeAddOrEdit(myState);
		}
	}
	
	public void editBudget(String columnName, String newValue, String oldValue) throws SQLException{
		DBObject myDB = new DBObject();
		myDB.connect();
		ResultSet mySet = null;
		System.out.println(oldValue);
		mySet = myDB.executeQuery("Select id from (select * from budgethassubcategory union select * from budgethascategory) as R where R.name = '"+ oldValue + "'");
		mySet.next();
		String id = Integer.toString((Integer)mySet.getObject(1));
		if(columnName.equals("CURRENTYEARAMOUNT")){
			String myState = "Update budget set currentyearamount = " + newValue + " where id = " + id;
			database.executeAddOrEdit(myState);
		}
	}
}
